//
//  MYMachineViewController.m
//  MiYuan_iOS
//
//  Created by zhangyongbing on 2018/5/14.
//  Copyright © 2018年 zhangyongbing. All rights reserved.
//

#import "MYMachineViewController.h"
#import "MY_getVendingListHttpApi.h"
#import "MachineDetailTableCell.h"
#import "MYChangeMachineDetailViewController.h"

@interface MYMachineViewController ()
@property (nonatomic,copy) NSString   *strMachineId;
@property (nonatomic,strong) NSMutableDictionary    *dic_data;
@property (nonatomic,weak) IBOutlet   UITableView *tabMain;
@property (nonatomic,weak) IBOutlet   UIView          *viewHead;
@end

@implementation MYMachineViewController


-(instancetype)initWithMachineId:(NSString *)sid
{
    self = [super initWithNibName:@"MYMachineViewController" bundle:nil];
    if (self) {
        self.strMachineId = sid;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *version= [UIDevice currentDevice].systemVersion;
    
    if(version.doubleValue >=11.0) {
        
    }else{
        // 针对 11.0 以下的iOS系统进行处理
        for (NSLayoutConstraint *constraint in self.viewHead.superview.constraints) {
            if (constraint.firstItem == self.viewHead && constraint.firstAttribute == NSLayoutAttributeTop) {
                constraint.constant = 20;
            }
        }
        
    }
    // Do any additional setup after loading the view from its nib.
    [self getDataFromServices];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getDataFromServices
{
    [MY_getVendingListHttpApi MYGet_VendingMachineDetailWithID:self.strMachineId completionHandler:^(OCJBaseResponceModel *responseModel) {
        [self.dic_data removeAllObjects];
        [self.dic_data addEntriesFromDictionary:responseModel.ocjDic_data];
        [self.tabMain reloadData];
    }];
}

- (IBAction)onBackButtonPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)onChangeButtonPressed:(id)sender
{
    MYChangeMachineDetailViewController *ctrl = [[MYChangeMachineDetailViewController alloc] initWithMachineDetailDic:self.dic_data];
    [self.navigationController pushViewController:ctrl animated:YES];
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat height = 55;
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifiernoe = @"MachineDetailTableCell";
    MachineDetailTableCell *cell  = (MachineDetailTableCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifiernoe];
    
    if (!cell)
    {
        [tableView registerNib:[UINib nibWithNibName:@"MachineDetailTableCell" bundle:nil] forCellReuseIdentifier:cellIdentifiernoe];
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifiernoe];
    }
    switch (indexPath.row) {
        case 0:
        {
            NSString *jobNumber = [NSString stringWithFormat:@"%@",[self.dic_data objectForKey:@"jobNumber"]];
            if (![jobNumber stringIsValid]) {
                jobNumber = @"";
            }
            [cell setTitle:@"司员编号" Subtitle:jobNumber];
            break;
        }
        case 1:
        {
            NSString *phone = [NSString stringWithFormat:@"%@",[self.dic_data objectForKey:@"phone"]];
            
            if (![phone stringIsValid]) {
                phone = @"";
            }
            [cell setTitle:@"手机号码" Subtitle:phone];
            break;
        }case 2:
        {
            NSString *number = [NSString stringWithFormat:@"%@",[self.dic_data objectForKey:@"number"]];
            if (![number stringIsValid]) {
                number = @"";
            }
            [cell setTitle:@"机器编号" Subtitle:number];
            break;
        }case 3:
        {
            NSString *controlNumber = [NSString stringWithFormat:@"%@",[self.dic_data objectForKey:@"controlNumber"]];
            if (![controlNumber stringIsValid]) {
                controlNumber = @"";
            }
            [cell setTitle:@"工控编号" Subtitle:controlNumber];
            break;
        }case 4:
        {
            NSString *pointName = [NSString stringWithFormat:@"%@",[self.dic_data objectForKey:@"pointName"]];
            if (![pointName stringIsValid]) {
                pointName = @"";
            }
            [cell setTitle:@"机器地点" Subtitle:pointName];
            break;
        }case 5:
        {
            NSString *launchDate = [NSString stringWithFormat:@"%@",[self.dic_data objectForKey:@"launchDate"]];
            if (![launchDate stringIsValid]) {
                launchDate = @"";
            }
            [cell setTitle:@"投放时间" Subtitle:launchDate];
            break;
        }case 6:
        {
            break;
        }
            
        default:
            break;
    }
    return cell;
}


- (NSMutableDictionary *)dic_data
{
    if (_dic_data == nil) {
        _dic_data = [[NSMutableDictionary alloc] init];
    }
    return _dic_data;
}

@end
